'use strict';
class showModal {
  constructor(that, options = {}) {
    this.__Ctrl = that;
    this.reset(options);
  }
  _animation = null;
  static showModalOptionsDafult = {
    title: '',
    content: '',
    showCancel: true,
    cancelText:'取消',
    cancelColor:'#000',
    confirmText: '确定',
    confirmColor:'#f40',
    success: function () { },
    fail: function () { }
  }
  reset(options) {
    var _this = this;
    _this.opts = Object.assign(showModal.showModalOptionsDafult, options);
    _this.__Ctrl.lmfModal_success = function () {
      _this.__close();
      _this.opts.success({ confirm: true });
    };
    _this.__Ctrl.lmfModal_fail = function () {
      _this.__close();
      _this.opts.fail({ confirm: false });
    };

  }

  open() {
    // 展示
    this.__Ctrl.setData({
      lmfModal: {
        title: this.opts.title,
        iconType: this.opts.icon,
        content: this.opts.content,
        showCancel: this.opts.showCancel,
        cancelText: this.opts.cancelText,
        cancelColor: this.opts.cancelColor,
        confirmText: this.opts.confirmText,
        confirmColor: this.opts.confirmColor,
        showModalStatus: true,
        animationData: this.getAnimation().opacity(1).step().export()
      }
    });
  }
  __close() {
    this.__Ctrl.setData({
      'lmfModal.showModalStatus': false,
      'lmfModal.animationData': this.getAnimation().opacity(0).step().export()
    }
    );

  }
  getAnimation() {
    if (!this._animation) {
      this._animation = wx.createAnimation({
        duration: 200, //动画时长 
        timingFunction: "linear", //线性 
        delay: 0 //0则不延迟 
      });
    }
    return this._animation
  }
}


module.exports = {
  showModal: function (options = {}) {
    // if (!__showModal[this.__webviewId__]) {
    //   __showModal[this.__webviewId__] = new showModal(this, options)
    // } else {
    //   __showModal[this.__webviewId__].reset(options);
    // }
    var __showModal = new showModal(this, options);
    __showModal.open();
  }
};